package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseLevelGauge;
import com.workcell.workdata.xthings.entity.BaseTimeClock;
import com.workcell.workdata.xthings.entity.BaseTvBox;
import com.workcell.workdata.xthings.mapper.BaseTimeClockMapper;
import com.workcell.workdata.xthings.service.BaseTimeClockService;
import org.springframework.stereotype.Service;

/**
 *
 * @author zhangguangrui
 * @since 2023-03-14
 */
@Service
public class BaseTimeClockServiceImpl extends ServiceImpl<BaseTimeClockMapper, BaseTimeClock> implements BaseTimeClockService {

    @Override
    public Boolean updateBaseTimeClock(BaseTimeClock baseTimeClock) {

        Assert.isTrue(ObjectUtil.isNotEmpty(baseTimeClock.getId()),"ID不能为空");
        // 查询该考勤机状态
        BaseTimeClock beforeTimeClock = this.getById(baseTimeClock.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(beforeTimeClock),"无该条记录，请核实您的操作");
        Assert.isTrue(HardwareStateEnum.DRAF.getCode().equals(beforeTimeClock.getState()),"状态为草稿才能修改");
        return this.updateById(baseTimeClock);


    }

    @Override
    public Boolean delById(Long id) {
        Assert.isTrue(ObjectUtil.isNotEmpty(id),"ID不能为空");
        //查询设备状态
        BaseTimeClock beforeTimeClock = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotEmpty(beforeTimeClock),"无该条记录，请核实您的操作");
        if (HardwareStateEnum.INUSE.getCode().equals(beforeTimeClock.getState())
             || HardwareStateEnum.SUSPEND.getCode().equals(beforeTimeClock.getState())) {
            Assert.isTrue(false,"当状态为使用中或者暂停中时，不能删除该条记录");
        }

        //删除记录
        return this.removeById(id);
    }

    @Override
    public Page<BaseTimeClock> timeClockQueryPage(BaseVo baseVo) {
        LambdaQueryWrapper<BaseTimeClock> queryWrapper = new QueryWrapper<BaseTimeClock>().lambda().
        and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()),  i -> i.like(BaseTimeClock::getName,baseVo.getKeyWord())
                .or()
                .like(BaseTimeClock::getManufacturer,baseVo.getKeyWord()))
                .eq(ObjectUtil.isNotEmpty(baseVo.getState()),BaseTimeClock::getState,baseVo.getState())
                .and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime()),
                        timeClock -> timeClock.between(BaseTimeClock::getCreateTime,baseVo.getStartTime(),baseVo.getEndTime()));
        Page<BaseTimeClock> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        return this.page(page,queryWrapper);
    }

    @Override
    public Boolean updateStateById(BaseTimeClock baseTimeClock) {

        Assert.isTrue(ObjectUtil.isNotEmpty(baseTimeClock.getId()),"ID不能为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(baseTimeClock.getState()),"状态不能为空");
        // 查询该打卡机的状态
        BaseTimeClock baseTimeClockOld = this.getById(baseTimeClock.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(baseTimeClockOld),"无该条记录，请核实您的操作");
        if (HardwareStateEnum.INUSE.getCode().equals(baseTimeClockOld.getState())
                || HardwareStateEnum.SUSPEND.getCode().equals(baseTimeClockOld.getState())){
            if (HardwareStateEnum.DRAF.getCode().equals(baseTimeClock.getState())){
                Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
            }
        }
        return this.updateById(baseTimeClock);

    }
}
